library(haven)
library(cregg)
library(plyr)
library(gridExtra)
library(ggplot2)

# Read in data
tess <- read_dta("tess.dta")

# Create factors

tess$PARTISANSHIP_ <- factor(mapvalues(as.character(tess$PARTISANSHIP_),1:3,
                                       c("Democrat","Republican","Independent")),levels=c("Independent","Democrat","Republican"))

tess$disagree <- factor(mapvalues(tess$disagree,c(0,1),c("Agree","Disagree")),levels=c("Agree","Disagree"))

tess$GENDER_ <- as.factor(mapvalues(as.character(tess$GENDER_),c(1,2),c("Man","Woman")))

tess$AGE_ <- factor(mapvalues(as.character(tess$AGE_),1:5,
                              c("28","37","46","55","63")),levels=c("28","37","46","55","63"))

tess$ETHNICITY_ <- factor(mapvalues(as.character(tess$ETHNICITY_),1:4,
                                    c("White","Hispanic","African-American","Asian")),levels=c("White","Hispanic","African-American","Asian"))

tess$CAREER_ <- relevel(as.factor(mapvalues(as.character(tess$CAREER_),1:8,
                                            c("Hospitality","Technology","Small business owner","Law",
                                              "Health care","Education","Agriculture","Retail"))),ref="Hospitality")

tess$RELIGION_ <- factor(mapvalues(as.character(tess$RELIGION_),1:7,
                                   c("None","Jewish","Catholic","Mainline protestant",
                                     "Evangelical protestant","Mormon","Muslim")),levels=c("None","Jewish","Catholic","Mainline protestant",
                                                                                           "Evangelical protestant","Mormon","Muslim"))

tess$EDUCATION_ <- factor(mapvalues(as.character(tess$EDUCATION_),1:4,
                                    c("High school diploma","Associate degree","Bachelor degree",
                                      "Graduate degree")),levels= c("High school diploma","Associate degree","Bachelor degree",
                                                                    "Graduate degree"))

tess$FAVORITEHOBBY_ <- relevel(as.factor(mapvalues(as.character(tess$FAVORITEHOBBY_),1:9,
                                                   c("Music","Food","Reading","Video games","Travel","Exercise",
                                                     "Arts and crafts","Watching sports","Watching TV"))),ref="Music")

tess$mc_factor <- as.factor(tess$mc)

tess$inparty_factor <- relevel(as.factor(mapvalues(tess$inparty,c(0,1),c("Not copartisan","Copartisan"))),ref="Not copartisan")

tess$outparty_factor <- relevel(as.factor(mapvalues(tess$outparty,c(0,1),c("Not outpartisan","Outpartisan"))),ref="Not outpartisan")

tess$Ideology <- relevel(as.factor(mapvalues(as.numeric(tess$PartyID7),1:7,c(rep("Liberal",3),NA,rep("Conservative",3)))),ref="Liberal")

# Main effect of value disagreement: Figure 3
temp <- tess[,c("polavg","disagree","inparty_factor","outparty_factor","GENDER_","AGE_","ETHNICITY_","CAREER_","RELIGION_","EDUCATION_","FAVORITEHOBBY_","CaseId")]
f1 <- polavg ~ disagree + inparty_factor + outparty_factor + GENDER_ + AGE_ + ETHNICITY_ + CAREER_ + RELIGION_ + EDUCATION_ + FAVORITEHOBBY_
amces <- cj(na.omit(temp),f1,id=~CaseId,feature_labels=list(disagree="VALUES", inparty_factor="IN-PARTY",outparty_factor="OUT-PARTY",
                                                   GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                   EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"))
pdf("polavg_maineffect_R.pdf",height=8,width=9)
plot(amces,xlim=c(-0.15,0.1)) +
  theme(legend.position = "none")
dev.off()

# Main effects, for liberals and conservatives separately: Figure A8 in Online Appendix
temp <- tess[,c("polavg","disagree","inparty_factor","outparty_factor","GENDER_","AGE_","ETHNICITY_","CAREER_","RELIGION_","EDUCATION_","FAVORITEHOBBY_","CaseId","Ideology")]
amces <- cj(na.omit(temp),f1,id=~CaseId,feature_labels=list(disagree="VALUES", inparty_factor="IN-PARTY",outparty_factor="OUT-PARTY",
                                                   GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                   EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"),
            by=~Ideology)
pdf("polavg_byideo_R.pdf",height=8,width=9)
plot(amces, group = "Ideology", vline = 0.5)
dev.off()

# Main effects, using partisanship control: Figure A9 in Online Appendix
f1 <- polavg ~ disagree + PARTISANSHIP_ + GENDER_ + AGE_ + ETHNICITY_ + CAREER_ + RELIGION_ + EDUCATION_ + FAVORITEHOBBY_
amces <- cj(tess,f1,id=~CaseId,feature_labels=list(disagree="VALUES", PARTISANSHIP_="PARTISANSHIP",
                                                   GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                   EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"))
pdf("polavg_partisanship_R.pdf",height=8,width=9)
plot(amces,xlim=c(-0.15,0.1))
dev.off()

# Main effects, for each outcome separately: Figure A10 in Online Appendix
favtemp <- tess
disttemp <- tess
trusttemp <- tess
favtemp$Outcome <- "Favorability"
disttemp$Outcome <- "Neighbor"
trusttemp$Outcome <- "House"
favtemp$Outcome <- as.factor(favtemp$Outcome)
disttemp$Outcome <- as.factor(disttemp$Outcome)
trusttemp$Outcome <- as.factor(trusttemp$Outcome)

fav_amces <- cj(favtemp,feel01 ~ disagree + inparty_factor + outparty_factor + GENDER_ + AGE_ + ETHNICITY_ + CAREER_ + RELIGION_ + EDUCATION_ + FAVORITEHOBBY_
                ,id=~CaseId,feature_labels=list(disagree="VALUES",inparty_factor="IN-PARTY",outparty_factor="OUT-PARTY",
                                                   GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                   EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"),by=~Outcome)
dist_amces <- cj(disttemp,neighbor01 ~ disagree + inparty_factor + outparty_factor + GENDER_ + AGE_ + ETHNICITY_ + CAREER_ + RELIGION_ + EDUCATION_ + FAVORITEHOBBY_
                 ,id=~CaseId,feature_labels=list(disagree="VALUES",inparty_factor="IN-PARTY",outparty_factor="OUT-PARTY",
                                                           GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                           EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"),by=~Outcome)
trust_amces <- cj(trusttemp,house01 ~ disagree + inparty_factor + outparty_factor + GENDER_ + AGE_ + ETHNICITY_ + CAREER_ + RELIGION_ + EDUCATION_ + FAVORITEHOBBY_,
                  id=~CaseId,feature_labels=list(disagree="VALUES",inparty_factor="IN-PARTY",outparty_factor="OUT-PARTY",
                                                           GENDER_="GENDER",AGE_="AGE",ETHNICITY_="ETHNICITY",CAREER_="CAREER",RELIGION_="RELIGION",
                                                           EDUCATION_="EDUCATION",FAVORITEHOBBY_="HOBBY"),by=~Outcome)

pdf("polavg_eachoutcome_R.pdf",height=8,width=9)
plot(rbind(fav_amces,dist_amces,trust_amces), group = "Outcome")
dev.off()


